package com.bobi.tfca.ui.mainnavigation.home.activity

import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.ImageView
import android.widget.Toast
import androidx.navigation.findNavController
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bobi.tfca.R
import com.bobi.tfca.base.BaseFragment
import com.bobi.tfca.constants.PageElementIdConstant
import com.bobi.tfca.databinding.FragmentActivityBinding
import com.bobi.tfca.network.client.AppCache
import com.bobi.tfca.network.response.ActivityResponse
import com.bobi.tfca.ui.adapter.ActivityAdapter

class ActivityFragment: BaseFragment<FragmentActivityBinding, ActivityFragmentVM>(
    FragmentActivityBinding::inflate,
    ActivityFragmentVM::class.java,
    true
) {
    private lateinit var activityRV: RecyclerView
    private lateinit var noDataIV: ImageView

    @SuppressLint("ResourceType", "UseRequireInsteadOfGet")
    override fun initFragment(
        binding: FragmentActivityBinding,
        viewModel: ActivityFragmentVM?,
        savedInstanceState: Bundle?
    ) {
        // 初始化RV
        initRecyclerView(binding)

        noDataIV = binding.ivNoData

        // 监听viewmodel
        viewModel!!.apply {
            activities.observe(this@ActivityFragment) {
                val adapter = activityRV.adapter as ActivityAdapter
                adapter.setItemList(it)
                adapter.notifyDataSetChanged()
            }
        }

        // 设置菜单为监听的
        publicViewModel!!.apply {
            visiblePageElements.observe(this@ActivityFragment) {
                binding.ivAddNew.visibility = getElementVisibility(PageElementIdConstant.FRAGMENT_ACTIVITY_IV_ADD_NEW)
            }
        }

        // 设置跳转组织活动
        binding.ivAddNew.setOnClickListener {
            requireActivity().findNavController(R.id.app_nav).navigate(R.id.action_mainNavigationFragment_to_organizeActivityFragment)
        }
    }

    /**
     * 初始化RV
     */
    private fun initRecyclerView(
        binding: FragmentActivityBinding
    ) {
        // 获取RecyclerView实例
        activityRV = binding.rvActivity

        // 获得所有活动
        val activityList : ArrayList<ActivityResponse> = arrayListOf()
        // 设置布局管理器
        val linearLayoutManager = LinearLayoutManager(binding.root.context)
        linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
        activityRV.layoutManager = linearLayoutManager
        activityRV.addItemDecoration(
            DividerItemDecoration(
                binding.root.context,
                LinearLayoutManager.VERTICAL
            )
        )
        val articleAdapter = ActivityAdapter(this, activityList)

        // 设置监听器
        articleAdapter.setOnItemClickListener(object : ActivityAdapter.OnItemClickListener {
            override fun onItemClick(position: Int) {
                // 设置跳转详情
                publicViewModel!!.activity.value = articleAdapter.activityList[position]
                // 是否登录，未登录则不跳转，并提示请登录或注册
                val loginStatus = AppCache.checkLoginStatus()
                if (loginStatus) {
                    requireActivity().findNavController(R.id.app_nav)
                        .navigate(R.id.action_mainNavigationFragment_to_activityDetailsFragment)
                } else {
                    Toast.makeText(requireActivity(), "请登录或注册", Toast.LENGTH_SHORT).show()
                }
            }
        })
        // 绑定Adapter
        activityRV.adapter = articleAdapter
    }

    override fun resumeFragment(viewModel: ActivityFragmentVM?) {
        super.resumeFragment(viewModel)
        viewModel!!.findAllActivities(publicViewModel!!, this, noDataIV)
    }
}